package com.zy.je.book.module.initimport.service.impl;

import cn.hutool.core.date.StopWatch;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zy.je.book.module.initimport.calibre.entity.*;
import com.zy.je.book.module.initimport.service.InitImportService;
import com.zy.je.book.module.tmp.entity.TmpAuthor;
import com.zy.je.common.core.utils.SpringContextHolder;
import com.zy.je.common.core.utils.idworker.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.File;
import java.util.List;

@Slf4j
@Service
public class InitImportServiceImpl implements InitImportService {
    static String BOOK_DS = "book";
    static String CALIBRE_DS = "calibre";
    @Resource
    DynamicRoutingDataSource dynamicRoutingDataSource;

    @Override
    public void importFormLocalCalibreDb(String dbPath) {
//        DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);

        String batchNo = IdWorker.nextSeq36();
        //1、先保存author、language、publisher、rating、series、tag
        saveBasicInfo(batchNo);
        //2、更新相应的link的数据
        //4、入库书籍信息

        //切换数据源
        DynamicDataSourceContextHolder.push(BOOK_DS);
        dynamicRoutingDataSource.determineDataSource();

        TmpAuthor tmpAuthor = new TmpAuthor();
        tmpAuthor.setName("xxxx");
        tmpAuthor.insert();

        List<TmpAuthor> list = tmpAuthor.selectAll();

        tmpAuthor.deleteById(1804073380388724737L);
        log.debug("{}",list);

        //切换数据源
        DynamicDataSourceContextHolder.push(CALIBRE_DS);
        dynamicRoutingDataSource.determineDataSource();

        //解析calibre db
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("load books");
        List<AuthorsEntity> authorEntityList = new AuthorsEntity().selectAll();
        List<BooksAuthorsLinkEntity> bkAuthorLinkEntityList = new BooksAuthorsLinkEntity().selectAll();
        List<BooksEntity> bookEntityList = new BooksEntity().selectAll();
        List<CommentsEntity> bkCommentEntityList = new CommentsEntity().selectAll();
        List<DataEntity> bkDataEntityList = new DataEntity().selectAll();
        List<IdentifiersEntity> bkIdentifierEntityList = new IdentifiersEntity().selectAll();
        List<LanguagesEntity> languageEntityList = new LanguagesEntity().selectAll();
        List<BooksLanguagesLinkEntity> bkLanguageLinkEntityList = new BooksLanguagesLinkEntity().selectAll();
        List<PublishersEntity> publishersEntityList = new PublishersEntity().selectAll();
        List<BooksPublishersLinkEntity> bkPublisherLinkEntityList = new BooksPublishersLinkEntity().selectAll();
        List<RatingsEntity> ratingEntityList = new RatingsEntity().selectAll();
        List<BooksRatingsLinkEntity> bkRatingLinkEntities = new BooksRatingsLinkEntity().selectAll();
        List<SeriesEntity> seriesEntitieList = new SeriesEntity().selectAll();
        List<BooksSeriesLinkEntity> bkSeriesLinkEntitieList = new BooksSeriesLinkEntity().selectAll();
        List<TagsEntity> tagEntityList = new TagsEntity().selectAll();
        List<BooksTagsLinkEntity> bkTagLinkEntitieList = new BooksTagsLinkEntity().selectAll();
        List<PreferencesEntity> preferenceEntityList = new PreferencesEntity().selectAll();
        List<LastReadPositionsEntity> lastReadEntityList = new LastReadPositionsEntity().selectAll();
        stopWatch.stop();



        //进行数据转换
        stopWatch.start("converter data");

        stopWatch.stop();

        //校验文件是否存在 --可选

        //生成文件唯一编码
        stopWatch.start("calc book sha512");
        stopWatch.stop();

        //去重入库
        stopWatch.start("save to db");
        stopWatch.stop();

        log.info(stopWatch.prettyPrint());
    }

    private void saveBasicInfo(String batchNo) {
        List<AuthorsEntity> authorEntityList = new AuthorsEntity().selectAll();
        List<LanguagesEntity> languageEntityList = new LanguagesEntity().selectAll();
        List<PublishersEntity> publishersEntityList = new PublishersEntity().selectAll();
        List<RatingsEntity> ratingEntityList = new RatingsEntity().selectAll();
        List<SeriesEntity> seriesEntitieList = new SeriesEntity().selectAll();
        List<TagsEntity> tagEntityList = new TagsEntity().selectAll();



    }


    private String genFileUniqueId(String filePath) {
//        String filePath = "D:/tmp/燕闲四适.20卷.明孙丕显编.王基校.明万历三十九年刊本.pdf"; // 替换为你的文件路径
        File file = new File(filePath);
        return (new Digester(DigestAlgorithm.SHA512)).digestHex(file);
    }
}
